package com.bestcem.xm.txtanls.dao.impl;//package com.bestcem.xm.txtanls.dao.impl;
//
//import cn.hutool.core.collection.CollUtil;
//import com.bestcem.xm.txtanls.dao.AnalysisProjectDao;
//import com.bestcem.xm.txtanls.dao.dto.PageDTO;
//import com.bestcem.xm.txtanls.entity.mongo.AnalysisProject;
//import com.bestcem.xm.txtanls.grpc.service.param.PageParam;
//import com.bestcem.xm.txtanls.grpc.service.param.TextFieldParam;
//import com.bestcem.xm.txtanls.grpc.service.param.analysis.UpdateAnalysisProjectParam;
//import com.bestcem.xm.txtanls.util.enums.AnalysisStatusEnum;
//import com.bestcem.xm.txtanls.util.enums.TrainStatusEnum;
//import com.bestcem.xm.common.core.uitls.DateUtil;
//import com.bestcem.xm.util.GRpcUtil;
//import com.mongodb.client.result.DeleteResult;
//import com.mongodb.client.result.UpdateResult;
//import org.apache.commons.lang3.StringUtils;
//import org.bson.types.ObjectId;
//import org.springframework.data.domain.Sort;
//import org.springframework.data.mongodb.core.MongoTemplate;
//import org.springframework.data.mongodb.core.query.Criteria;
//import org.springframework.data.mongodb.core.query.Query;
//import org.springframework.data.mongodb.core.query.Update;
//import org.springframework.stereotype.Repository;
//import org.springframework.util.CollectionUtils;
//import org.springframework.util.ObjectUtils;
//
//import javax.annotation.Resource;
//import javax.validation.ConstraintViolation;
//import java.util.ArrayList;
//import java.util.Collections;
//import java.util.List;
//import java.util.Set;
//
//import static com.bestcem.xm.txtanls.util.CollectionName.TXTANLS_ANLSPROJECT;
//import static com.bestcem.xm.txtanls.util.ColumnConstant.AnalysisProject.*;
//import static com.bestcem.xm.txtanls.util.ColumnConstant.AnalysisProject.TextField.Column.FULL_ID;
//import static com.bestcem.xm.txtanls.util.ColumnConstant.Base.UPDATED_DT;
//import static com.bestcem.xm.txtanls.util.ColumnConstant.Base._ID;
//
///**
// * @author liheng
// * @version v1.0
// * @date 2021/6/28 14:08
// */
////@Repository
//public class AnalysisProjectDaoImpl /*implements AnalysisProjectDao*/ {
//
//    @Resource
//    private MongoTemplate mongoTemplate;
//
////    @Override
//    public AnalysisProject findById(String projectId) {
//        Query query = new Query();
//        query.addCriteria(Criteria.where(_ID).is(new ObjectId(projectId)));
//        return mongoTemplate.findOne(query, AnalysisProject.class, TXTANLS_ANLSPROJECT);
//    }
//
////    @Override
//    public AnalysisProject findOneByStatus(int status) {
//        Query query = new Query();
//        query.addCriteria(Criteria.where(STATUS).is(status));
//        return mongoTemplate.findOne(query, AnalysisProject.class, TXTANLS_ANLSPROJECT);
//    }
//
//
////    @Override
//    public List<AnalysisProject> findAllByStatus(int status) {
//        Query query = new Query();
//        query.addCriteria(Criteria.where(STATUS).is(status));
//        return mongoTemplate.find(query, AnalysisProject.class, TXTANLS_ANLSPROJECT);
//    }
//
////    @Override
//    public AnalysisProject findByNameAndCreator(String projectName, String userId) {
//        Query query = new Query();
//        query.addCriteria(Criteria.where(NAME).is(projectName));
//        query.addCriteria(Criteria.where(CREATOR).is(userId));
//        return mongoTemplate.findOne(query, AnalysisProject.class, TXTANLS_ANLSPROJECT);
//    }
//
////    @Override
//    public PageDTO<AnalysisProject> findUserAllProject(String userId, PageParam pageParam) {
//        Query query = new Query();
//        query.addCriteria(Criteria.where(CREATOR).is(userId));
//        query.addCriteria(new Criteria().orOperator(Criteria.where(SURVEY_ID).exists(false), Criteria.where(SURVEY_ID).is(null)));
//        query.addCriteria(Criteria.where(STATUS).in(
//                AnalysisStatusEnum.INACTIVE.getIndex(), AnalysisStatusEnum.ACTIVE.getIndex(), AnalysisStatusEnum.FINISHED.getIndex(),
//                AnalysisStatusEnum.FAILED.getIndex(), AnalysisStatusEnum.CREATED.getIndex()));
//        Long total = mongoTemplate.count(query, TXTANLS_ANLSPROJECT);
//        int skipSize = pageParam.getPage() * pageParam.getSize();
//        query.skip(skipSize);
//        query.limit(pageParam.getSize());
//        query = query.with(Sort.by(Sort.Direction.DESC, CREATED_DT));
//        List<AnalysisProject> result = mongoTemplate.find(query, AnalysisProject.class, TXTANLS_ANLSPROJECT);
//        return new PageDTO<>(result, total);
//    }
//
////    @Override
//    public PageDTO<AnalysisProject> findOrganizationAllProject(String orgId, PageParam pageParam) {
//        Query query = new Query();
//        query.addCriteria(Criteria.where(ORG_ID).is(new ObjectId(orgId)));
//        query.addCriteria(new Criteria().orOperator(Criteria.where(SURVEY_ID).exists(false), Criteria.where(SURVEY_ID).is(null)));
//        query.addCriteria(Criteria.where(STATUS).in(
//                AnalysisStatusEnum.INACTIVE.getIndex(), AnalysisStatusEnum.ACTIVE.getIndex(), AnalysisStatusEnum.FINISHED.getIndex(),
//                AnalysisStatusEnum.FAILED.getIndex(), AnalysisStatusEnum.CREATED.getIndex()));
//        Long total = mongoTemplate.count(query, TXTANLS_ANLSPROJECT);
//        int skipSize = pageParam.getPage() * pageParam.getSize();
//        query.skip(skipSize);
//        query.limit(pageParam.getSize());
//        query = query.with(Sort.by(Sort.Direction.DESC, CREATED_DT));
//        List<AnalysisProject> result = mongoTemplate.find(query, AnalysisProject.class, TXTANLS_ANLSPROJECT);
//        return new PageDTO<>(result, total);
//    }
//
////    @Override
//    public List<AnalysisProject> findAllProjectByFieldId(String textFieldId) {
//        Query query = new Query();
//        query.addCriteria(Criteria.where(FULL_ID).is(textFieldId));
//        return mongoTemplate.find(query, AnalysisProject.class, TXTANLS_ANLSPROJECT);
//    }
//
////    @Override
//    public AnalysisProject findSurveyAnalysisProject(String surveyId) {
//        Query query = new Query();
//        query.addCriteria(Criteria.where(SURVEY_ID).is(new ObjectId(surveyId)));
//        return mongoTemplate.findOne(query, AnalysisProject.class, TXTANLS_ANLSPROJECT);
//    }
//
////    @Override
//    public AnalysisProject insertProject(AnalysisProject initEntity) {
//        initEntity.setCreatedDate(DateUtil.getMongoDate());
//        initEntity.setUpdatedDate(DateUtil.getMongoDate());
//        return mongoTemplate.insert(initEntity, TXTANLS_ANLSPROJECT);
//    }
//
////    @Override
//    public UpdateResult updateProject(UpdateAnalysisProjectParam projectParam) {
//        Set<ConstraintViolation<Object>> violations = GRpcUtil.validateParam(projectParam);
//        if (!CollectionUtils.isEmpty(violations)) {
//            return UpdateResult.unacknowledged();
//        }
//
//        Query query = new Query();
//        query.addCriteria(Criteria.where(_ID).is(new ObjectId(projectParam.getProjectId())));
//        Update update = new Update();
//        update.set(NAME, projectParam.getName());
//        update.set(TABLE_ID, new ObjectId(projectParam.getTableId()));
//        update.set(TABLE_NAME, projectParam.getTableName());
//        List<AnalysisProject.TextField> textFields = new ArrayList<>();
//        for (TextFieldParam textField : projectParam.getTextFields()) {
//            textFields.add(new AnalysisProject.TextField(textField.getId(), textField.getValue()));
//        }
//        update.set(TEXT_FIELD_LIST, textFields);
//        update.set(TIME_FIELD_ID, projectParam.getTimeFieldId());
//        update.set(TIME_FIELD_NAME, projectParam.getTimeFieldName());
//        update.set(UPDATED_DT, DateUtil.getMongoDate());
//        return mongoTemplate.upsert(query, update, TXTANLS_ANLSPROJECT);
//    }
//
////    @Override
//    public UpdateResult updateAnalysisProjectStatus(String projectId, int status) {
//        Query query = new Query();
//        query.addCriteria(Criteria.where(_ID).is(new ObjectId(projectId)));
//        Update update = new Update();
//        update.set(STATUS, status);
//        update.set(UPDATED_DT, DateUtil.getMongoDate());
//        return mongoTemplate.upsert(query, update, TXTANLS_ANLSPROJECT);
//    }
//
////    @Override
//    public UpdateResult updateAnalysisProjectProgress(String projectId, TrainStatusEnum status, long trainProgress) {
//        Query query = new Query();
//        query.addCriteria(Criteria.where(_ID).is(new ObjectId(projectId)));
//        Update update = new Update();
//        update.set(TRAIN_STATUS, status.getName());
//        update.set(TRAIN_PROGRESS, trainProgress);
//        update.set(UPDATED_DT, DateUtil.getMongoDate());
//        return mongoTemplate.upsert(query, update, TXTANLS_ANLSPROJECT);
//    }
//
////    @Override
//    public UpdateResult updateProjectFieldList(String projectId, List<AnalysisProject.TextField> textFields) {
//        Query query = new Query();
//        query.addCriteria(Criteria.where(_ID).is(new ObjectId(projectId)));
//        Update update = new Update();
//        update.set(TEXT_FIELD_LIST, textFields);
//        update.set(UPDATED_DT, DateUtil.getMongoDate());
//        return mongoTemplate.upsert(query, update, TXTANLS_ANLSPROJECT);
//    }
//
////    @Override
//    public UpdateResult updateProjectTimeField(String projectId, String timeFieldId, String timeFieldName) {
//        Query query = new Query();
//        query.addCriteria(Criteria.where(_ID).is(new ObjectId(projectId)));
//        Update update = new Update();
//        update.set(TIME_FIELD_ID, timeFieldId);
//        update.set(TIME_FIELD_NAME, timeFieldName);
//        update.set(UPDATED_DT, DateUtil.getMongoDate());
//        return mongoTemplate.upsert(query, update, TXTANLS_ANLSPROJECT);
//    }
//
////    @Override
//    public UpdateResult updateTableNameByTableId(String tableId, String tableName) {
//        Query query = new Query();
//        query.addCriteria(Criteria.where(TABLE_ID).is(new ObjectId(tableId)));
//        Update update = new Update();
//        update.set(TABLE_NAME, tableName);
//        update.set(UPDATED_DT, DateUtil.getMongoDate());
//        return mongoTemplate.updateMulti(query, update, TXTANLS_ANLSPROJECT);
//    }
//
////    @Override
//    public UpdateResult updateTimeFieldByTimeId(String timeFieldId, String timeFieldName) {
//        Query query = new Query();
//        query.addCriteria(Criteria.where(TIME_FIELD_ID).is(timeFieldId));
//        Update update = new Update();
//        update.set(TIME_FIELD_NAME, timeFieldName);
//        update.set(UPDATED_DT, DateUtil.getMongoDate());
//        return mongoTemplate.updateMulti(query, update, TXTANLS_ANLSPROJECT);
//    }
//
////    @Override
//    public DeleteResult deleteProject(String projectId) {
//        Query query = new Query();
//        query.addCriteria(Criteria.where(_ID).is(new ObjectId(projectId)));
//        return mongoTemplate.remove(query, TXTANLS_ANLSPROJECT);
//    }
//
////    @Override
//    public List<String> listFiledId(String projectId, String fieldId) {
//        ArrayList<String> fieldIds = new ArrayList<>(10);
//        if (StringUtils.isBlank(fieldId)) {
//            Query query = new Query(Criteria.where(_ID).is(new ObjectId(projectId)));
//            query.fields().include(TEXT_FIELD_LIST);
//            AnalysisProject one = mongoTemplate.findOne(query, AnalysisProject.class);
//            if (ObjectUtils.isEmpty(one) || CollUtil.isEmpty(one.getTextFieldList())) {
//                return Collections.emptyList();
//            }
//            List<AnalysisProject.TextField> textFieldList = one.getTextFieldList();
//            for (AnalysisProject.TextField textField : textFieldList) {
//                fieldIds.add(textField.getId());
//            }
//        } else {
//            fieldIds.add(fieldId);
//        }
//        return fieldIds;
//    }
//}
